import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.lang.NumberFormatException;

class DayFinderGeorgian {
	
	enum Month {
			JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER;
		
		static int getMonthNumber(String monthName) {
			return (Month.valueOf(monthName.toUpperCase()).ordinal()+1);
		}
	}

	private static final int DAYS[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
	private static final String DAY[] = {"Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday"}; //01 Jan 1583 was a Saturday. So DAY[1] is Saturday
	private static final int YEAR_OF_APPLYING_GEORGIAN_CALENDAR = 1582;
	private int currentDay; 
	private int currentMonth=-1; 
	private int currentYear=-1;
	private String date = "13 May 1983";
	
	public DayFinderGeorgian(){
		try{
			date = new ConsoleReaderUtility().readString("Enter date in following format \"dd month yyyy\": ");
		}
		catch (IOException e) {
			System.out.println("There was an exception trying to read a provided date... The default one will be used "+this.date);
		}
	}
	
	public static void main(String[] args){
		DayFinderGeorgian dayFinderTester = new DayFinderGeorgian();
		int dayIndex;

		dayFinderTester.parseInputDate();
		
		dayIndex = dayFinderTester.calculateDayIndex();
		
		System.out.println("The day for the "+dayFinderTester.getDate()+" is "+DAY[dayIndex]);
		
	}
	
	private int calculateDayIndex(){
		int daysPast = 0;
		
		for (int year=YEAR_OF_APPLYING_GEORGIAN_CALENDAR+1; year<currentYear; year++){
			if(isLeapYear(year)){
				daysPast+=366;
			} else {
				daysPast+=365;
			}
		}
		
		for (int month=0; month<currentMonth-1; month++) {
			daysPast+=DAYS[month];
		}
		
		daysPast+=currentDay;
		
		return daysPast%7;
	}
	

	
	private void parseInputDate() {
		String[] dateParts = date.split(" ");
		if (dateParts.length != 3){
			System.out.println("Wrong date format. Expecting to have 3 elements");
			System.exit(1);
		}
		setCurrentYear(dateParts[2]);
		setCurrentMonth(dateParts[1]);
		setCurrentDay(dateParts[0]);
	}
	
	private void setCurrentYear(String year){
		try{
			currentYear = Integer.parseInt(year);
		} catch (NumberFormatException e) {
			System.out.println("Year should be an Integer value");
			System.exit(1);
		}
		
		if (currentYear<0) {
			System.out.println("Year should be positive");
			System.exit(1);
		}
		
		if (currentYear<YEAR_OF_APPLYING_GEORGIAN_CALENDAR) {
			System.out.println("Year should be greater than "+YEAR_OF_APPLYING_GEORGIAN_CALENDAR);
			System.exit(1);
		}

	}

	private void setCurrentMonth(String month){
		currentMonth = Month.getMonthNumber(month.toUpperCase());
	}
	
	private void setCurrentDay(String day){
		try{
			currentDay = Integer.parseInt(day);
		} catch (NumberFormatException e) {
			System.out.println("Day should be an Integer value");
		}
		
		if ( currentDay<1 || currentDay > getMaxDaysForMonth() ) {
			System.out.println("Day should be in a 1.."+getMaxDaysForMonth()+" range");
			System.exit(1);
		}

	}
	
	private int getMaxDaysForMonth(){
		return getMaxDaysForMonth(currentYear, currentMonth);
	}
	
	private int getMaxDaysForMonth(int year, int month){
		if (month==2) {
			if (isLeapYear(year)){
				return 29;
			} else {
				return 28;
			}
		}	
		
		return DAYS[month-1];
	}
	
	private boolean isLeapYear(int year){
		return (year%400==0 || (year%4==0 && year%100!=0) ); // each 400th year is a leap year; each 4th year is a leap unless year is multiple by 100 (year%100 == 0).
	}
	
	private String getDate(){
		return date;
	}
}
